<?php


namespace App\Controller\App;


use App\Base\BaseController4App;
use App\Entity\SalesUserJuan;
use App\Entity\User;
use App\Repository\BannerRepository;
use App\Repository\SalesCutRepository;
use App\Repository\SalesJuanRepository;
use App\Repository\SiteOilCategoryRepository;
use App\Repository\SiteRepository;
use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/app")
 */
class AppLoginController extends BaseController4App
{
    /**
     * @Route("/login", name="AppMiniAppLogin")
     */
    public function AppMiniAppLogin(
        UserRepository $repository
        ,SalesJuanRepository $salesJuanRepository
    )
    {
        $req = $this->getRequestData();
        $openid = $req["openid"]??"";
        $unionid = $req["unionid"]??$openid;
        $name = $req["name"]??"";
        $avatar = $req["avatar"]??"";


        $user = $repository->findOneByWx($openid);
        if (!$user) {
            $user = new User();
            $user->setName($name)
                ->setCreateAt(new \DateTime())
                ->setOpenid($openid)
                ->setUnionid($unionid)
                ->setPhone("")
                ->setAvatar($avatar)
                ->setIsBan(false)
                ->setApiToken($openid);

            $parent = $req["parent"]??"";
            $parentUser = $repository->findOneByWx($parent);
            if ($parentUser) {
                $user->setParent($parentUser);
                //发放优惠券
//            cat = 6 邀请奖励
                $juan = $salesJuanRepository->findOneBy(array("cat.id"=>6,"isUse"=>true));
                if ($juan){

                    $userjuan = new SalesUserJuan();
                    $userjuan->setStatus(1)
                        ->setCreateAt(new \DateTime())
                        ->setUser($parentUser)
                        ->setJuan($juan)
//                    ->setExpireAt(new \DateTime(strtotime("+ {$juan->getExpireDay()} days")))
                        ->setExpireAt(new \DateTime(("+ {$juan->getExpireDay()} days")))
                    ;

                    $this->myEm()->persist($userjuan);
                    $this->myEm()->flush();
                }
            }

            $this->myEm()->persist($user);
            $this->myEm()->flush();

            //发放优惠券
//            cat = 5 注册奖励
            $juan = $salesJuanRepository->findOneBy(array("cat.id"=>5,"isUse"=>true));
            if ($juan) {
                $userjuan = new SalesUserJuan();
                $userjuan->setStatus(1)
                    ->setCreateAt(new \DateTime())
                    ->setUser($user)
                    ->setJuan($juan)
//                ->setExpireAt(new \DateTime(strtotime("+ {$juan->getExpireDay()} days")))
                    ->setExpireAt(new \DateTime(("+ {$juan->getExpireDay()} days")))
                ;

                $this->myEm()->persist($userjuan);
                $this->myEm()->flush();
            }

        }
        return $this->json_out(1,"OK",$user);
    }

}
